package com.david.dao;

import com.david.entity.Article;
import com.david.entity.Category;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ArticleDao {

    @Select("select * from article where isDelete = 'false'")
    List<Article> findAll();

    @Select("select * from article where isDelete = 'false' and parentid = #{parentId}")
    List<Article> GetArticleListByParentId(int parentId);

    @Select("select * from article where isDelete = 'false' and parentid in (select id from category where parentid = #{parentId})")
    List<Article> GetArticleListInParentId(int parentId);

    @Select("select * from article where isDelete = 'false' and parentId = #{parentId} order by id desc")
    List<Article> findByParentId(int parentId);

    @Select("select * from article where isDelete= 'false' and title like concat('%',#{title},'%') and parentId in (select id from category where route like concat('%,',#{parentId},',%')) order by updateTime desc")
    List<Article> GetAllArticleByParentId(@Param("parentId") int parentId, @Param("title") String title);

    @Select("select * from category where isDelete = 'false' and route like concat('%,',#{parentId},',%') order by id desc")
    List<Category> GetCategoryAndSelfByParentId(int parentId);

    @Select("select count(*) from article where isDelete= 'false' and title like concat('%',#{title},'%') and  parentId in (select id from category where route like concat('%,',#{parentId},',%')) order by updateTime desc")
    int GetAllArticleByParentIdCount(@Param("parentId") int parentId, @Param("title") String title);

    @Select("select * from article where id = #{id} and isDelete = 'false' ")
    Article GetArticleById(Integer id);

    @Insert("insert into article (title,titleEn,realTitle,description,imgSrc,content,createTime,updateTime,isDelete,parentId,sort,imgSrc2,url,fileSrc,viewCount,downloadCount) values (#{title},#{titleEn},#{realTitle},#{description},#{imgSrc},#{content},#{createTime},#{updateTime},#{isDelete},#{parentId},#{sort},#{imgSrc2},#{url},#{fileSrc},#{viewCount},#{downloadCount}) ")
    int AddArticle(Article article);

    @Update("update article set title = #{title},titleEn=#{titleEn},realTitle=#{realTitle},description=#{description},imgSrc=#{imgSrc},content=#{content},updateTime=#{updateTime},parentId=#{parentId},url=#{url},imgSrc2=#{imgSrc2},fileSrc=#{fileSrc} where id = #{id}")
    int updateArticle(Article article);

    @Update("update article set isDelete = true where id = #{id}")
    int deleteArticle(Integer id);

    @Select("select * from article where parentId = #{parentId} and isdelete = 'false' and id < #{id} order by id desc limit 1;")
    Article Next(@Param("parentId") Integer parentId, @Param("id") Integer id);

    @Select("select * from article where parentId = #{parentId} and isdelete = 'false' and id > #{id} order by id asc limit 1;")
    Article Prev(@Param("parentId") Integer parentId, @Param("id") Integer id);

    @Update("update article set viewCount = viewCount + 1 where id = #{id}")
    int updateViewCount(int id);
    @Update("update article set downloadCount = downloadCount + 1 where id = #{id}")
    int downloadCount(int id);
}
